﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace InstallParser
{
    //Very simple console app to update the Farm parser configuration
    //as well as the local DOCPARSE.XML file
    class Program
    {
        const string PARSER_PROGID = "Version3.SharePoint.ZipParser";
        const string PARSER_FILE_EXTENSION = "zip";

        static void Main(string[] args)
        {
            bool xmlOnly = false;
            bool installParser = true;
            string argumentOne = string.Empty;
            string argumentTwo = string.Empty;
            if (args.Length > 0) argumentOne = args[0].ToLower().Trim();
            if (args.Length > 1) argumentTwo = args[1].ToLower().Trim();

            if (("uninstall" == argumentOne) || ("uninstall" == argumentTwo))installParser = false;
            if (("xmlonly" == argumentOne) || ("xmlonly" == argumentTwo)) xmlOnly = true;

            if (installParser)
            {
 
                if (!xmlOnly)
                {
                    SPFarm localFarm = SPFarm.Local;
                    SPWebService sharePointService = localFarm.Services.GetValue<SPWebService>();

                    if (sharePointService.PluggableParsers.ContainsKey(PARSER_FILE_EXTENSION))
                    {
                        sharePointService.PluggableParsers.Remove(PARSER_FILE_EXTENSION);
                    }
                    SPDocumentParser customParser = new SPDocumentParser(PARSER_PROGID, PARSER_FILE_EXTENSION);
                    sharePointService.PluggableParsers.Add(PARSER_FILE_EXTENSION, customParser);
                    Console.WriteLine("Farm configured");
                    sharePointService.Update();
                }
                UpdateDocParseXML(true);
            }
            else
            {
                if (!xmlOnly)
                {
                    SPFarm localFarm = SPFarm.Local;
                    SPWebService sharePointService = localFarm.Services.GetValue<SPWebService>();

                    if (sharePointService.PluggableParsers.ContainsKey(PARSER_FILE_EXTENSION))
                    {
                        sharePointService.PluggableParsers.Remove(PARSER_FILE_EXTENSION);
                    }
                    sharePointService.Update();
                    Console.WriteLine("Farm un-configured");
                }
                UpdateDocParseXML(false);
            }
           
            Console.WriteLine("Press enter..");
            Console.ReadLine();
        }

        static void UpdateDocParseXML(bool addEntry)
        {
            string docParseXmlFilePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonProgramFiles);
            docParseXmlFilePath += "\\Microsoft Shared\\web server extensions\\12\\CONFIG\\DOCPARSE.XML";
            if(System.IO.File.Exists(docParseXmlFilePath))
            {
                XmlDocument parseDoc = new XmlDocument();
                parseDoc.Load(docParseXmlFilePath);
                XmlNode docParsers = parseDoc.SelectSingleNode("//docParsers");
                if (null != docParsers)
                {
                    XmlNode existingEntry = docParsers.SelectSingleNode("docParser[@name=\"" + PARSER_FILE_EXTENSION + "\"]");
                    if (addEntry)
                    {
                        if (null != existingEntry)
                        {
                            docParsers.RemoveChild(existingEntry);
                        }
                        XmlNode newEntry = parseDoc.CreateElement("docParser");
                        XmlAttribute entryName = parseDoc.CreateAttribute("name");
                        entryName.InnerText = PARSER_FILE_EXTENSION;
                        XmlAttribute entryProgID = parseDoc.CreateAttribute("ProgId");
                        entryProgID.InnerText = PARSER_PROGID;

                        newEntry.Attributes.Append(entryName);
                        newEntry.Attributes.Append(entryProgID);
                        docParsers.AppendChild(newEntry);
                        parseDoc.Save(docParseXmlFilePath);
                        Console.WriteLine("Entry added to DOCPARSE.XML");
                    }
                    else
                    {
                        if (null != existingEntry)
                        {
                            docParsers.RemoveChild(existingEntry);
                            parseDoc.Save(docParseXmlFilePath);
                            Console.WriteLine("Entry removed from  DOCPARSE.XML");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Could not parse DOCPARSE.XML");
                }
            }
            else
            {
                Console.WriteLine("Could not find DOCPARSE.XML");
            }
        }
    }
}
